#include "StdAfx.h"
#include "user_cache_test.h"
#include <boost\thread.hpp>

user_cache_test::user_cache_test()
	:data_count_(500)
{
}
void user_cache_test::SetUp()
{
	init_data();
}
void user_cache_test::TearDown()
{
	clear_data();
}

void user_cache_test::init_data()
{
	string qry;
	string qry_start = "INSERT INTO t_user(account, password) values('jackong";
	string qry_end = "', '123456')";
	DBM->prepare_manual_commit();
	for (size_t i = 0; i < data_count_; ++i)
	{
		qry.append(qry_start);
		qry.append(boost::lexical_cast<string>(i));
		qry.append(qry_end);
		DBM->query_non_lock(qry.c_str());
		qry.clear();
	}
	DBM->commit();
	ucache->init();

	//mock server setup (io service pool run)
	ucache->setup_sync_timer();
	boost::thread t(boost::bind(&io_service_pool::run, is_pool));
}
void user_cache_test::clear_data()
{
	string qry;
	string qry_start = "DELETE FROM t_user WHERE account = 'jackong";
	string qry_end = "'";
	DBM->prepare_manual_commit();
	for (size_t i = 0; i < data_count_; ++i)
	{
		qry.append(qry_start);
		qry.append(boost::lexical_cast<string>(i));
		qry.append(qry_end);
		DBM->query_non_lock(qry.c_str());
		qry.clear();
	}
	DBM->commit();
	ucache->clear();

	is_pool->stop();
}


TEST_F(user_cache_test, find_in_cache)
{
	const User* user = ucache->find("jackong1");
	ASSERT_FALSE(NULL == user);	
	EXPECT_EQ("jackong1", user->account());
}

TEST_F(user_cache_test, find_in_db)
{
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	DBM->query("INSERT INTO t_user(account, password) values('jackong', '123456')");
	ucache->clear();
	ucache->init(500);
	const User* user = ucache->find("jackong");
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	ASSERT_FALSE(NULL == user);
	EXPECT_EQ("jackong", user->account());
}

TEST_F(user_cache_test, find_in_sync)
{
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	User ur;
	ur.set_action(CREATE);
	ur.set_account("jackong");
	ur.set_password("123456");
	EXPECT_TRUE(ucache->add(ur));

	const User* user = ucache->find("jackong");
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
	ASSERT_FALSE(NULL == user);
	EXPECT_EQ("jackong", user->account());
}


TEST_F(user_cache_test, add_nonkey)
{
	User user;
	user.set_password("123456");
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_FALSE(ucache->add(user));
}

TEST_F(user_cache_test, add_nonpwd)
{
	User user;
	user.set_account("jackong");
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_FALSE(ucache->add(user));
}

TEST_F(user_cache_test, add_error_action)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(DEL);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_FALSE(ucache->add(user));
}

TEST_F(user_cache_test, add_exist)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));
	EXPECT_FALSE(ucache->add(user));
	EXPECT_TRUE(ucache->remove(user.account()));
}

TEST_F(user_cache_test, add_ok)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));
	EXPECT_TRUE(ucache->remove(user.account()));
}

TEST_F(user_cache_test, remove_not_exist)
{
	EXPECT_FALSE(ucache->remove("jackong"));
}

TEST_F(user_cache_test, remove_ok)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));
	EXPECT_TRUE(ucache->remove("jackong"));
}

TEST_F(user_cache_test, update_deleted)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));
	EXPECT_TRUE(ucache->remove(user.account()));
	user.set_action(UPDATE);
	EXPECT_FALSE(ucache->update(user));
}

TEST_F(user_cache_test, update_unexist)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(UPDATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_FALSE(ucache->update(user));
}

TEST_F(user_cache_test, update_ok)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));
	user.set_action(UPDATE);
	EXPECT_TRUE(ucache->update(user));
	EXPECT_TRUE(ucache->remove("jackong"));
}

TEST_F(user_cache_test, sync_create)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));
	Sleep(ucache->sec_interval() * 1100);
	MYSQL_RES res;
	DBM->query("SELECT account FROM t_user WHERE account = 'jackong'", &res);
	EXPECT_EQ(1, res.row_count);
	EXPECT_TRUE(ucache->remove("jackong"));
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
}

TEST_F(user_cache_test, sync_del)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));
	EXPECT_TRUE(ucache->remove(user.account()));
	Sleep(ucache->sec_interval() * 1100);
	MYSQL_RES res;
	DBM->query("SELECT account FROM t_user WHERE account = 'jackong'", &res);
	EXPECT_EQ(0, res.row_count);
}

TEST_F(user_cache_test, sync_update)
{
	User user;
	user.set_account("jackong");
	user.set_password("123456");
	user.set_action(CREATE);
	Pair* p = user.add_attributes();
	p->set_name("name");
	p->set_value("chain");
	EXPECT_TRUE(ucache->add(user));

	user.set_action(UPDATE);
	user.set_password("654321");
	Pair* attr = user.mutable_attributes(0);
	attr->set_value("jack");

	EXPECT_TRUE(ucache->update(user));

	Sleep(ucache->sec_interval() * 1100);

	MYSQL_RES res;
	DBM->query("SELECT account FROM t_user WHERE account = 'jackong' and password = '654321' and name = 'jack'", &res);
	EXPECT_EQ(1, res.row_count);

	EXPECT_TRUE(ucache->remove("jackong"));
	DBM->query("DELETE FROM t_user WHERE account = 'jackong'");
}